<?php

/**
 * 用户模块
 * 
 * @author ShuangYa
 * @package EUser
 * @category Service
 * @link http://www.sylingd.com/
 * @copyright Copyright (c) 2015 ShuangYa
 * @license http://lab.sylingd.com/go.php?name=euser&type=license
 */

namespace euser\service;
use \sy\lib\YCookie;
use \sy\lib\YSecurity;
use \sy\lib\db\YMongoDB;

class User {
	protected static $_instance = NULL;
	public static function i() {
		if (self::$_instance === NULL) {
			self::$_instance = new self;
		}
		return self::$_instance;
	}
	public function __construct() {
	}
	/**
	 * 新增一个用户
	 * @access public
	 * @param array $user 用户信息
	 * @return string
	 */
	public function add($user) {
		$user['password'] = YSecurity::password($user['password']);
		$user['regTime'] = $_SERVER['REQUEST_TIME'];
		//ID由用户名生成
		$user['_id'] = YMongoDB::MongoID($this->genUid($user['name']));
		$r = YMongoDB::i()->select('user')->insert($user);
		return strval($user['_id']);
	}
	/**
	 * 获取一个用户
	 * @access public
	 * @param string $id 用户ID
	 * @return array
	 */
	public function get($id) {
		return YMongoDB::i()->select('user')->getOne(['_id' => YMongoDB::MongoID($id)]);
	}
	/**
	 * 验证一个用户名/密码
	 * @access public
	 * @param string $user
	 * @param string $password
	 * @return boolean/string
	 */
	public function verify($user, $password) {
		if (!is_string($user) || !is_string($password)) {
			return FALSE;
		}
		$user = $this->get($this->genUid($user));
		if ($user['password'] === YSecurity::password($password)) {
			return strval($user['_id']);
		} else {
			return FALSE;
		}
	}
	/**
	 * 验证Cookie中的token是否有效
	 * 若有效，则返回token信息，无效则返回FALSE
	 * @access public
	 * @return boolean/string
	 */
	public function verifyCookie($requestId = NULL) {
		//如果存在token，则校验token是否有效
		if (YCookie::exists('token', $requestId) && YCookie::exists('tverify', $requestId)) {
			$token = YCookie::get('token', $requestId);
			$tverify = YCookie::get('tverify', $requestId);
			if (FALSE === ($t = $this->getToken($token))) {
				return FALSE;
			}
			if ($tverify !== $t['tverify']) {
				return FALSE;
			}
			return $t;
		} else {
			return FALSE;
		}
	}
	/**
	 * 生成新的token
	 * @access public
	 * @param string $uid
	 * @param int $expire_day token有效期，默认为7天
	 * @return string
	 */
	public function createToken($uid, $expire_day = 7) {
		$tverify = md5(uniqid(time(), TRUE));
		$token = [
			'user' => $uid,
			'tverify' => $tverify,
			'expire' => $_SERVER['REQUEST_TIME'] + 3600 * 24 * $expire_day
		];
		$r = YMongoDB::i()->select('token')->insert($token);
		return [$r[0], $tverify];
	}
	/**
	 * 获取token
	 * 若验证通过，则返回token信息，否则返回FALSE
	 * @access public
	 * @param string $token
	 * @return array
	 */
	public function getToken($token) {
		if (strlen($token) !== 24) {
			return FALSE;
		}
		try {
			$r = YMongoDB::i()->select('token')->getOne(['_id' => YMongoDB::MongoID($token)]);
		} catch (\Exception $e) {
			return FALSE;
		}
		if ($r === NULL) {
			return FALSE;
		}
		if ($r['expire'] <= $_SERVER['REQUEST_TIME']) {
			//token过期
			YMongoDB::i()->select('token')->delete(['_id' => YMongoDB::MongoID($token)]);
			return FALSE;
		}
		return $r;
	}
	public function genUid($uname) {
		return hash('fnv164', $uname) . hash('adler32', $uname);
	}
}
